package com.pjnkj.thunder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjnkj.thunder.common.json.Body;
import com.pjnkj.thunder.common.json.Meta;
import com.pjnkj.thunder.common.utils.UploadImgUtils;
import com.pjnkj.thunder.entity.Order;
import com.pjnkj.thunder.entity.Orderverify;
import com.pjnkj.thunder.service.OrderService;
import com.pjnkj.thunder.service.OrderverifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.pjnkj.thunder.common.json.Meta.CODE_470;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pjnkj
 * @since 2022-02-12
 */
@RestController
@RequestMapping("/thunder/orderverify")
public class OrderverifyController {
    @Autowired
    OrderverifyService orderverifyService;

    @Autowired
    OrderService orderService;

    /**
     * 提交订单
     *
     * @param orderverify
     */
    @PostMapping("submitmessage")
//    public Body submitmessage(@RequestBody Orderverify orderverify) {
    public Body submitmessage(String orderid, String name, String ordernumber, String money, String img1, String img2, String img3) {
        Orderverify o = new Orderverify();
        o.setMoney(money);
        o.setImg1(img1);
        o.setImg2(img2);
        o.setImg3(img3);
        o.setOrdernumber(ordernumber);
        o.setName(name);
        o.setOrderid(Integer.valueOf(orderid));
        Order byId = orderService.getById(orderid);
        if (byId != null) {
            o.setTaskid(byId.getTaskid());
            o.setShopid(byId.getShopid());
            byId.setStatus(1);
            byId.setPaymoney(Integer.valueOf(money));
            orderService.saveOrUpdate(byId);
        } else {

        }
        if (orderverifyService.saveOrUpdate(o)) {
            return Body.newInstance("上传成功");
        } else {
            return Body.newInstance(Meta.CODE_400, "上传失败");
        }

    }

    /**
     * 上传图片
     *
     * @param orderverify
     */
    @PostMapping("upverifyimg")
    public Body upverifyimg(MultipartFile[] file) {
        String files = null;
        try {
            files = UploadImgUtils.uploadFiles(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (files != null && !"".equals(files)) {
            return Body.newInstance(files);
        } else {
            return Body.newInstance(Meta.CODE_400, "上传失败");
        }

    }


    /**
     * 删除数据
     *
     * @param orderverify
     */
    @PostMapping("delete")
    public Body delete(Integer id) {
        Boolean flag = orderverifyService.removeById(id);
        if (flag == true) {
            return Body.newInstance("删除成功");
        }
        return Body.newInstance(Meta.CODE_400, "删除失败");
    }

    /**
     * 增加数据
     *
     * @param orderverify
     */
    @PostMapping("create")
    public Body create(@Validated(Orderverify.class) @RequestBody Orderverify orderverify) {
        Boolean flag = orderverifyService.saveOrUpdate(orderverify);
        if (flag == true) {
            return Body.newInstance("保存成功");
        }
        return Body.newInstance(Meta.CODE_400, "保存失败");
    }

    /**
     * 修改数据
     *
     * @param orderverify
     */
    @PostMapping("update")
    public Body update(@Validated(Orderverify.class) @RequestBody Orderverify orderverify) {
        Boolean flag = orderverifyService.updateById(orderverify);
        if (flag == true) {
            return Body.newInstance("修改成功");
        }
        return Body.newInstance(Meta.CODE_400, "修改失败");
    }

    /**
     * 查询数据
     *
     * @param
     */
    @PostMapping("find")
    public Body find(String id) {
        Orderverify info = orderverifyService.getById(id);
        if (info != null) {
            return Body.newInstance(info);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }


    /**
     * 查询数据list
     *
     * @param
     */
    @PostMapping("findlist")
    public Body findlist() {
        List<Orderverify> list = orderverifyService.list();
        if (list != null && list.size() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }

    /**
     * 分页查询数据
     *
     * @param
     */
    @PostMapping("page")
    public Body page(Integer current, Integer size) {
        Page<Orderverify> pg = new Page<Orderverify>(current, size);
        IPage<Orderverify> list = orderverifyService.page(pg);
        if (list != null && list.getSize() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }
}
